const state = {
  favorites: JSON.parse(localStorage.getItem('favorites') || '[]')
}

const mutations = {
  ADD_FAVORITE(state, item) {
    state.favorites.push(item)
    localStorage.setItem('favorites', JSON.stringify(state.favorites))
  },
  REMOVE_FAVORITE(state, id) {
    state.favorites = state.favorites.filter(item => item.id !== id)
    localStorage.setItem('favorites', JSON.stringify(state.favorites))
  }
}

const actions = {
  addToFavorite({ commit }, item) {
    commit('ADD_FAVORITE', item)
  },
  removeFromFavorite({ commit }, id) {
    commit('REMOVE_FAVORITE', id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}